$('document').ready(function() {
	$('#chat_field').val("Say something");
	$('#chat_field').click(function() {if ($(this).val() == "Say something") $(this).val('');});
	$('#chat_field').blur(function() {if ($(this).val() == '') $(this).val("Say something");});
	$('#chat_send').click(function(){updateChat();});
	$('#chat_field').bind('keypress', function(e) {
		var code = (e.keyCode ? e.keyCode : e.which);		
		if(code == 13) updateChat();
	});
	$('#new_game_button').click(function(){
		if (confirm("Are you sure?")) {
			restartGame();
		}
	});
	$('#fold_button').click(function() {doFold();});
	$('#raise_button').click(function() {doRaise();});
	$('#call_button').click(function() {doCall();});
	$('#footer_buttons').hide();
	getGame();
});

function updateChat() {
	if ($('#chat_field').val() != '') {
		var new_row = $('#chat_field').val();
		$('#chat_field').val('');
		addToChat(new_row);
	}
	return false;
}

function addToChat(message) {
	$('#conversation').append(">>"+message+"<br />");
    $('#conversation').attr({
        scrollTop: $('#conversation').attr('scrollHeight')
    });
}

function getGame() {
	$.getJSON('game', {action:"get_game"},function(data) {		
		updatePlayers(data);
	});
}

function restartGame() {
	$('span[id^=dealer_]').hide();
	$.getJSON('game',{action:"restart_game"}, function(data) {
		addToChat("Started new game");
		updatePlayers(data);
	});
}

function updatePlayers(data) {
	//set player scores
	var current_data, name;
	$('td[id^=player_score_]').each(function() {
		current_data = data[$(this).attr('id')];
		if ($(this).html() != current_data) {
			name = $('span#player_'+($(this).attr('id')).charAt($(this).attr('id').length-1)+'_name').html();
			addToChat(name+" has score "+current_data);
			$(this).html(data[$(this).attr('id')]);
		}
	});
	//set player statuses	
	$('td[id^=player_status_]').each(function() {		
		current_data = data[$(this).attr('id')];
		if ($(this).html() != current_data) {
			name = $('span#player_'+($(this).attr('id')).charAt($(this).attr('id').length-1)+'_name').html();			
			addToChat(name+" did action: "+current_data);
			$(this).html(data[$(this).attr('id')]);
		}		
	});
	
	for (i=0;i<10;i++){
		$('div#player_'+i).removeClass('player_active');
	}
	$('div#player_cards').html('<img src="./cards/'+data['person_card1']+'.png" />'+'<img src="./cards/'+data['person_card2']+'.png" />');
	$('div#player_'+data['person_position']).addClass('player_active');
	$('span#dealer_'+data['dealer_position']).fadeIn('slow');
	if (data['person_to_call'] > 0){
		$('#call_button').html("<p>Call "+data['person_to_call']+"</p>");
	} else if (data['person_to_call'] == 0) {
		$('#call_button').html("<p>Check</p>");
	}
	$('div#footer_buttons').fadeIn('slow');
	$('div#footer_buttons div').addClass('button_active');
	
}

function doCall() {
	$.getJSON('game', {action:"person_call"},function(data) {		
		
	});
}
function doFold() {
	$.getJSON('game', {action:"person_fold"},function(data) {		
		
	});
}
function doRaise() {
	$.getJSON('game', {action:"person_raise"},function(data) {		
		
	});
}